/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils.middleware;

import java.util.WeakHashMap;

/**
 * 
 * @author selcuk
 */
public abstract class FlyweightFactory {

    protected WeakHashMap FLY_WEIGHT_DATA = new WeakHashMap();

    public FlyweightFactory() {
    }

    public Object getFlyweight(Object key) {
	if (!containsFlyweight(key)) {
	    Object data = loadFlyweight(key);
	    putFlyweight(key, data);
	}
	// return the single immutable copy with the given values
	return FLY_WEIGHT_DATA.get(key);
    }

    public void putFlyweight(Object key, Object value) {
	if (value != null) {
	    FLY_WEIGHT_DATA.put(key, value);
	}
    }

    public Object removeFlyweight(Object key) {
	return FLY_WEIGHT_DATA.remove(key);
    }

    public boolean containsFlyweight(Object key) {
	return FLY_WEIGHT_DATA.containsKey(key);
    }

    protected abstract Object loadFlyweight(Object key);
}
